/**
 * Created by Administrator on 2017/5/30 0030.
 */
$(function () {
    var num = $("#number").val();
    //小区域
    var smArea = $(".small_area");
    //大区域;
    var bgArea = $(".bigImg");
    //小图片;
    var smImg = $("#smImg");
    //大图片
    var bgImg = $("#bgImg");
    //设置小区域的大小;
    smArea.width(bgArea.width() * smImg.width() / bgImg.width());
    smArea.height(bgArea.height() * smImg.height() / bgImg.height());
    //倍数;
    var scale = bgImg.height()/bgArea.height() ;
    // console.log(scale);
    //小图四边的边界；
    var topSide = smImg.offset().top;
    var leftSide = smImg.offset().left;
    var rightSide = leftSide + smImg.width();
    var bottomSide = topSide + smImg.height();
    //移动事件；
    $(document).on("mousemove", function (e) {
        //如果鼠标在下图内;
        if (e.pageX > leftSide && e.pageX < rightSide && e.pageY > topSide && e.pageY < bottomSide) {
            smArea.show();
            bgArea.show();
            //移动小区域；
            var x = e.pageX - leftSide - smArea.width() / 2;
            var y = e.pageY - topSide - smArea.height() / 2;
            if(x<0){
                x = 0
            }
            if(x>smImg.width()-smArea.width()){
                x = smImg.width()-smArea.width()
            }
            if(y<0){
                y = 0
            }
            if(y>smImg.height()-smArea.height()){
                y = smImg.height()-smArea.height()
            }
            smArea.css({left: x, top: y});
            //大图移动：
            bgImg.css({left: -x * scale, top: -y * scale})
        } else {
            smArea.hide();
            bgArea.hide()
        }
    });
    //图片切换；
    $(".small_img ul li").on("mouseenter",function () {
        $(this).addClass("small").siblings("li").removeClass("small");
        var index = $(this).index() + 1;
        $("#smImg").attr("src","images/erji/"+ index +"big.jpg");
        $("#bgImg").attr("src","images/erji/"+ index +"big.jpg");
    });
    //增加数量和减少数量;
    //++
    $(".jia").on("click",function () {
       $(this).prev("input").val(parseInt($(this).prev("input").val())+1)
    });
    //--;
    $(".jian").on("click",function () {
        //大于1才能--
        if($(this).prev("span").prev("input").val()>1){
            $(this).prev("span").prev("input").val(parseInt($(this).prev("span").prev("input").val())-1);
        }
    });
    //评价切换;
    $(".check").on("click",function () {
        var temptIndex = $(this).index();
        //字体颜色变化：
        $(this).addClass("goods_on").siblings("li").removeClass("goods_on");
        //小三角
        $(this).find(".tri").removeClass("te").end().siblings("li").find(".tri").addClass("te");
        $(".che").eq(temptIndex).fadeIn().siblings("li").fadeOut();

    });
    //加入购物车事件;
    $("#join").on("click",function () {
        var num = $("#number").val();
        var detail = $("#detail");
        var goid = detail.data("goid");
        var goprice = detail.data("goprice");
        var goname = detail.data("goname");
        var uid = $.cookie("userid");
        var goprice1 = detail.data("goprice1");
        var goSimg = detail.data("gosimg");
        $.ajax({
            url:"./server/addcart.php",
            data:{
                goid:goid,
                num:num,
                goprice:goprice,
                goname:goname,
                uid:uid,
                goprice1:goprice1,
                goSimg:goSimg
            },
            type:"post",
            dataType:"json",
            success:function (res) {
                if (res.status == 1 || res.status == 3){
                    console.log(res);
                    $(".tips").show();
                    $("#goon").on("click",function () {
                        $(".tips").hide();
                    });
                    $(".buy").on("click",function () {
                        window.location.assign("car.html")
                    })
                }
            }
        })
    });
    //立即购买事件;
    $(".buy").eq(0).on("click",function () {
        var num = $("#number").val();
        var detail = $("#detail");
        var goid = detail.data("goid");
        var goprice = detail.data("goprice");
        var goname = detail.data("goname");
        var uid = $.cookie("userid");
        var goprice1 = detail.data("goprice1");
        var goSimg = detail.data("goSimg");
        $.ajax({
            url:"./server/addcart.php",
            data:{
                goid:goid,
                num:num,
                goprice:goprice,
                goname:goname,
                uid:uid,
                goprice1:goprice1,
                goSimg:goSimg
            },
            type:"post",
            dataType:"json",
            success:function (res) {
                if (res.status == 1 || res.status ==3){
                    window.location.assign("car.html")
                }
            }
        })
    });

    var ttt = $(".gotop");
    $(document).scroll(function () {
        var scrTop = $(document).scrollTop();
        if(scrTop>800){
            ttt.show()
        }else {
            ttt.hide()
        }
    });
    ttt.on("click",function () {
        $("body,html").animate({scrollTop:0})
    })

});